

package client;
// This file contains material supporting section 3.7 of the textbook:

import gui.GuestWindow;
import gui.LoginForm;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import logic.Message;
import logic.Reader;


import common.ChatIF;

/**
 * This class constructs the UI for a chat client.  It implements the
 * chat interface in order to activate the display() method.
 */

public class ClientConsole implements ChatIF,WindowListener
{
	Message m;
	//public GuestWindow aFrame;
	public static Reader reader;
	
	public LoginForm form;
  //Class variables *************************************************
  
  /**
   * The default port to connect on.
   */
  final public static int DEFAULT_PORT = 5555;
 
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */
  public static Client client;
   public static JFrame aFrame;
   public static JTextField ipTextField;
   public static  JTextField portTextField;
   public static JFrame ipWindow;
   public static String name=null;
   public static int id =-1;
   public static int role=-1;	//user's role
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientConsole(String host, int port) 
  {
    try 
    {
      client= new Client(host, port, this);
    } 
    catch(IOException exception) 
    {
      System.out.println("Error: Can't setup connection!"
                + " Terminating client.");
      System.exit(1);
    }
  }

  
  //Instance methods ************************************************
  
  /**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
   */
  public void accept() 
  {
    try
    {


      aFrame = new JFrame(); // create MainWindow
	
      aFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
      aFrame.setSize( 600,600 ); // set frame size
      aFrame.setLocation(300, 0);
      aFrame.setVisible( true ); // display frame
      aFrame.setTitle("Ort Braude Library");
      aFrame.setContentPane(new GuestWindow());
      aFrame.addWindowListener(this);
    }
 
    catch (Exception ex) 
    {
      System.out.println
        ("Unexpected error while reading from console!");
    }
  }

  /**
   * This method overrides the method in the ChatIF interface.  It
   * displays a message onto the screen.
   *
   * @param message The string to be displayed.
   */
  public void display(Object message) 
  {
	  m= (Message) message;
	  if(m.getCommand().equals("User Connected"))
	  {  
	  }
	
	//LoginForm.confirmationLable.setText(m.getCommand());
  }

  public static void main(String[] args) 
  {
    ipWindow=new JFrame();
    ipWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    ipWindow.setSize( 350,300 ); // set frame size
    ipWindow.setLocation(300, 0);
    ipWindow.setVisible( true ); // display frame
    JPanel ipPanel=new JPanel();
    ipPanel.setLayout( new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    JLabel ipLabel=new JLabel("IP:");
    JLabel portLabel=new JLabel("Port:");
    
    ipTextField=new JTextField(15);
    portTextField=new JTextField(15);
    ipTextField.setText("localhost");
    portTextField.setText("5555");
    JButton connectButton=new JButton("Connect");
    gbc.gridx=0;
	gbc.gridy=0;
    ipPanel.add(ipLabel,gbc);
    gbc.gridx=1;
	gbc.gridy=0;
    ipPanel.add(ipTextField,gbc);
    gbc.gridx=0;
	gbc.gridy=1;
	ipPanel.add(portLabel,gbc);
    gbc.gridx=1;
	gbc.gridy=1;
    ipPanel.add(portTextField,gbc);
    gbc.gridx=0;
   	gbc.gridy=2;
   	gbc.gridwidth=2;
    ipPanel.add(connectButton,gbc);
    ipWindow.setContentPane(ipPanel);
    ipWindow.pack();
    connectButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
		    ClientConsole Console= new ClientConsole(ipTextField.getText(), Integer.parseInt(portTextField.getText()));
		    Console.accept();  //Wait for console data 
		    ipWindow.dispose();
		}
	});
  }





public static void HandleMessageFromGui(Message m) {
	client.handleMessageFromClientUI(m);
}


@Override
public void windowActivated(WindowEvent arg0) {
	// TODO Auto-generated method stub
	
}


@Override
public void windowClosed(WindowEvent arg0) {
	System.out.printf("Logging out");
	Message m;
	if (reader!=null)
	{
		
		m=new Message("logout",reader);
		ClientConsole.HandleMessageFromGui(m);	
		m=(Message)ClientConsole.client.getMessage();
	}
	
}


@Override
public void windowClosing(WindowEvent arg0) {
	// TODO Auto-generated method stub
	
}


@Override
public void windowDeactivated(WindowEvent arg0) {
	// TODO Auto-generated method stub
	
}


@Override
public void windowDeiconified(WindowEvent arg0) {
	// TODO Auto-generated method stub
	
}


@Override
public void windowIconified(WindowEvent arg0) {
	// TODO Auto-generated method stub
	
}


@Override
public void windowOpened(WindowEvent arg0) {
	// TODO Auto-generated method stub
	
}
}
//End of ConsoleChat class
